//
//  WYTabBar.m
//  微博oc
//
//  Created by 王玉东 on 16/4/8.
//  Copyright © 2016年 wyd. All rights reserved.
//

#import "WYTabBar.h"
#import "UIView+frame.h"

@interface WYTabBar()

@property(nonatomic,weak) UIButton *addBtn;

@end

@implementation WYTabBar

#pragma mark - 创建tabBar中间的按钮
-(instancetype)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {
      //添加按钮
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        _addBtn = btn;
        
        [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        
        [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted ];
        
        [btn sizeToFit];
        
        [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        
        [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        
        [self addSubview:btn];
    
    }
    return self;
}

#pragma mark - 重新设置tabBar的frame
-(void)layoutSubviews{

    [super layoutSubviews]; //必须加载
    
    CGFloat W = self.W/5;
    CGFloat H = self.H;
    CGFloat Y = 0;
    //CGFloat X = 0;
    
    NSInteger Index = 0;
    
    self.addBtn.center = CGPointMake(self.W * 0.5, H * 0.5);
    
    for (UIView *obj in self.subviews) {
        
        if ([obj isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            
            obj.frame = CGRectMake(Index * W, Y, W, H);
            
            Index += Index == 1 ? 2 : 1;

        }
    }
    
}



@end
